/*
 * Copyright 2009-2010 the original author or authors.
 *
 * Licensed under the Apache license, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.internna.iwebjtracker.server.scm;

import java.util.Set;
import org.internna.iwebjtracker.project.model.Project;
import org.internna.iwebjtracker.project.model.Project.ProjectType;
import org.internna.iwebjtracker.project.validation.Errors;
import org.internna.iwebjtracker.project.validation.FreeFormProjectTypeValidator;
import org.internna.iwebjtracker.server.connection.ConnectionDetails;
import org.internna.iwebjtracker.server.connection.ServerConnection;
import org.internna.iwebjtracker.server.scm.model.ItemData;
import org.internna.iwebjtracker.server.scm.model.ItemInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import static org.internna.iwebjtracker.project.validation.Errors.*;

/**
 * A server useful when the project has no real server backend.
 *
 * @author Jose Noheda
 * @since 1.0
 */
@Service("nullSCMServer")
public final class NullSCMServer implements SourceControlManagementServer {

    public static final String NULL_SERVER_TYPE = "NullSCM";

    @Autowired @Qualifier("freeFormProjectValidator") private FreeFormProjectTypeValidator validator;

    /**
     * For testing purposes.
     *
     * @param validator
     */
    protected void setValidator(FreeFormProjectTypeValidator validator) {
        this.validator = validator;
    }

    /**
     * @param item any
     * @return null
     */
    @Override public ItemData loadItem(ItemInfo item) {
        return null;
    }

    /**
     * @param project any
     * @param url any
     * @return null
     */
    @Override public ItemData loadItem(Project project, String url) {
        return null;
    }

    /**
     * @param connection any
     * @param item any
     * @return null
     */
    @Override public ItemData loadItem(ServerConnection connection, ItemInfo item) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     * @param connection any
     * @param project any
     * @param url any
     * @param binary any
     * @return null
     */
    @Override public ItemData loadItem(ServerConnection connection, Project project, String url, boolean binary) {
        return null;
    }

    /**
     * @param project any
     * @param absolutePath any
     * @return null
     */
    @Override public ItemInfo getItemInfo(Project project, String absolutePath) {
        return null;
    }

    /**
     * @param project any
     * @param absolutePath any
     * @return null
     */
    @Override public Set getSubItems(Project project, String absolutePath) {
        return null;
    }

    /**
     * @param project any
     * @param itemInfo any
     * @return null
     */
    @Override public Set getSubItems(Project project, ItemInfo itemInfo) {
        return null;
    }

    /**
     * @param project any
     * @param absolutePath any
     * @return null
     */
    @Override public String getUnifiedDiff(Project project, String absolutePath) {
        return null;
    }

    /**
     * @param project any
     * @param itemInfo any
     * @return null
     */
    @Override public String getUnifiedDiff(Project project, ItemInfo itemInfo) {
        return null;
    }

    /**
     * @param connectionDetails any
     * @return null
     */
    @Override public ServerConnection openConnection(ConnectionDetails connectionDetails) {
        return null;
    }

    /**
     * @param connection any
     */
    @Override public void closeConnection(ServerConnection connection) {
        return;
    }

    /**
     * Any connection to this fake server is valid
     *
     * @param connectionDetails any
     * @return true
     */
    @Override public boolean testConnection(ConnectionDetails connectionDetails) {
        return true;
    }

    /**
     * No actual protocols supported.
     *
     * @return null
     */
    @Override public Set getSupportedProtocols() {
        return null;
    }

    /**
     * Identify this server as a null SCM.
     *
     * @return NullSCM
     */
    @Override public String getServerType() {
        return NULL_SERVER_TYPE;
    }

    /**
     * Accepts only FreeForm project types.
     *
     * @param project any
     * @return the i18nkey of the error
     */
    @Override public Errors validateProject(Project project) {
        if (project != null) {
            if (ProjectType.FreeForm.equals(project.getProjectType())) {
                return validator.validateProject(this, project);
            }
            return INVALID_PROJECT_TYPE;
        }
        return NULL_PROJECT_VALIDATION;
    }

}
